//
//  LXLMeTableViewController.m
//  真是不得姐啊
//
//  Created by 朱建鹏 on 16/8/24.
//  Copyright © 2016年 朱建鹏. All rights reserved.
//

#import "LXLMeTableViewController.h"
#import "LXLMeTableViewCell.h"
#import "LXLMeRecommendModel.h"
#import "ZYSRecommendTableVC.h"

#define isIOSVersion [[UIDevice currentDevice].systemVersion doubleValue]

@interface LXLMeTableViewController ()



@property (strong,nonatomic) NSArray *recommendArr;



@end

@implementation LXLMeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加leftBarButton
    [self addleftBarButtonItems];
    
    //获取数据
    [self addCellDataInfo];
    
    self.tableView.sectionFooterHeight = 10;
    self.tableView.sectionHeaderHeight = 0;
    //tableView的间距是35
    self.tableView.contentInset = UIEdgeInsetsMake(10 - 35, 0, 0, 0);
    
}



#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){
        return 1;
    }else if (section == 1){
        return 1;
    }else{
        return self.recommendArr.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSString *str = nil;
    
    LXLMeRecommendModel *model = self.recommendArr[indexPath.row];
    
    if(indexPath.section == 0){
        
        str = @"LXLMeLogInCell";
        
    }else if (indexPath.section == 1){
        
        str = @"LXLMeButtonCell";
        
    }else{
        
        str = @"LXLMeBaseCell";
    }
    
    LXLMeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str forIndexPath:indexPath];
    
    cell.model = model;
    
    cell.url = [NSURL URLWithString:@"http://d.api.budejie.com/op/square/bs0315-iphone-4.3/0-100.json"];
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        
        return 64;
        
    }else if (indexPath.section == 1){
        
        return 200;
        
    }else{
        
        return 64;
    }

}


#pragma mark - 点击Cell时触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
 
    if(indexPath == [NSIndexPath indexPathForRow:0 inSection:0]){
      
        
        
        
    }
    //----------------------------zys的华丽分割线-----------------------
    else if (indexPath.section == 2)
    {
        LXLMeRecommendModel *model = self.recommendArr[indexPath.row];
        
        //隐藏tabbar
        self.hidesBottomBarWhenPushed = YES;
        
        ZYSRecommendTableVC *recTabVC = [[ZYSRecommendTableVC alloc]init];
        
        recTabVC.theme_id = [model.theme_id integerValue];
        
        [self.navigationController pushViewController:recTabVC animated:YES];
        
        self.hidesBottomBarWhenPushed = NO;
        
    }
    
    //----------------------------zys的华丽分割线------------------------
}



#pragma mark - 添加navigationItem
- (void)addleftBarButtonItems{
    
    UIImage *imgNormal = nil;
    UIImage *imgHighlighted = nil;
    UIImage *imgSelected = nil;
    
    NSMutableArray *arrM = [NSMutableArray new];
    
    for (NSInteger i = 0; i < 2; i++) {
        
        UIButton *but = [[UIButton alloc]init];
        
        switch (i) {
            case 0:
                imgNormal = [UIImage imageNamed:@"mine-setting-icon"];
                imgHighlighted = [UIImage imageNamed:@"mine-setting-icon-click"];
                break;
            case 1:
                imgNormal = [UIImage imageNamed:@"mine-moon-icon"];
                imgHighlighted = [UIImage imageNamed:@"mine-moon-icon-click"];
                imgSelected = [UIImage imageNamed:@"mine-sun-icon"];
                break;
                
        }
        
        [but setImage:imgNormal forState:UIControlStateNormal];
        
        [but setImage:imgHighlighted forState:UIControlStateHighlighted];
        
        [but setImage:imgSelected forState:UIControlStateSelected];
        
        [but sizeToFit];
        
        but.tag = i;
        
        [but addTarget:self action:@selector(clickRightBarButtonItemsAction:) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *barBut = [[UIBarButtonItem alloc]initWithCustomView:but];
        
        [arrM addObject:barBut];
        
    }
    
    self.navigationItem.rightBarButtonItems = arrM.copy;
    
}
#pragma mark - navigationItem点击事件
- (void)clickRightBarButtonItemsAction:(UIButton *)sender{
    
    
    if(!sender.tag){//设置
        
        
    }else{//夜间模式‚
        
        sender.selected = !sender.selected;
        
    }
    
}

#pragma mark - 获取数据
- (void)addCellDataInfo{
    
  
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?a=tag_recommend&c=topic"]];
    
    [LXLMeRecommendModel LXLMeRecommendModelDataWithURL:url succeedBlock:^(NSArray *arr) {
        
        self.recommendArr = arr;
        
        [self.tableView reloadData];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"self.recommendArr %@",error);
        
    }];
    
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}@end
